package com.education.online.controller;


import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtil;
import com.education.online.feign.UserFeign;
import com.education.online.pojo.OLiveVideo;
import com.education.online.pojo.OLiveVideoList;
import com.education.online.pojo.OUserLive;
import com.education.online.service.OLiveVideoListService;
import com.education.online.service.OLiveVideoService;
import com.education.online.service.OUserLiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职业达人视频表
 *
 * @date 2020-06-04 15:17:49
 */
@Api(tags = {"PC--职业达人视频表"})
@RestController
@RequestMapping("/online/olivevideo")
public class OLiveVideoController {

    @Autowired
    private OLiveVideoService oLiveVideoService;

    @Autowired
    private OLiveVideoListService oLiveVideoListService;

    @Autowired
    private OUserLiveService oUserLiveService;

    @Autowired
    private UserFeign userFeign;


    @ApiOperation(value = "查询职业达人资源", httpMethod = "GET")
    @GetMapping("/selectList")
    public ApiReturnObject selectList(Integer menuId,Integer userId){
        OLiveVideo video = new OLiveVideo();
        video.setMenuId(menuId);
        List<OLiveVideo> liveVideoList = oLiveVideoService.selectOLiveVideoList(video);
        //查询视频标签
        setLabel(userId, liveVideoList);
        return new ApiReturnObject("200","success",liveVideoList);
    }

    //查询视频标签
    private void setLabel(Integer userId, List<OLiveVideo> liveVideoList) {
        for (OLiveVideo oLiveVideo : liveVideoList) {
            //根据发布者id查询发布者
            if (oLiveVideo.getUserId() != null){
                Map<String, String> map = userFeign.findByUserId(oLiveVideo.getUserId());
                oLiveVideo.setRelName(map.get("name"));
                oLiveVideo.setRelImage(map.get("image"));
            }else{
                oLiveVideo.setRelName("游客");//默认
                oLiveVideo.setRelImage("1579412171558.jpg");//默认
            }
            //查询图片或者视频集合
            OLiveVideoList oLiveVideoList = new OLiveVideoList();
            oLiveVideoList.setLiveId(oLiveVideo.getId());
            List<OLiveVideoList> videoLists = oLiveVideoListService.selectOLiveVideoListList(oLiveVideoList);
            oLiveVideo.setOLiveVideoList(videoLists);
            //处理时间
            oLiveVideo.setTime(DateUtil.dataTimeFormat(oLiveVideo.getCreateTime()));
            //判断当前用户是否收藏该达人视频
            islike(userId, oLiveVideo);
        }
    }

    @ApiOperation(value = "职业达人资源收藏", httpMethod = "GET")
    @GetMapping("/isLike")
    public ApiReturnObject isLike(Integer liveId,Integer userId){
        OUserLive oUserLive = new OUserLive();
        oUserLive.setUserId(userId);
        oUserLive.setLiveId(liveId);
        List<OUserLive> userLiveList = oUserLiveService.selectOUserLiveList(oUserLive);
        if (userLiveList.size() == 0){
            //查不到说明未收藏  需要收藏
            oUserLive.setCreatetime(new Date());
            oUserLiveService.insertOUserLive(oUserLive);
            Map<String, String> map = new HashMap<>();
            map.put("isLike", "1");
            return new ApiReturnObject("200","success",map);
        }else {
            //查到了说明已收藏  取消收藏
            oUserLiveService.deleteOUserLiveByUserIdAndLiveId(userId,liveId);
            Map<String, String> map = new HashMap<>();
            map.put("isLike", "0");
            return new ApiReturnObject("200","success",map);
        }
    }

    @ApiOperation(value = "我的收藏", httpMethod = "GET")
    @GetMapping("/myCollection")
    public ApiReturnObject myCollection(Integer userId){
        List<OLiveVideo> liveVideoList = oLiveVideoService.selectOLiveVideoByUserId(userId);
        //查询视频标签
        setLabel(userId, liveVideoList);
        return new ApiReturnObject("200","success",liveVideoList);
    }

    /**
     * 判断当前用户是否收藏该达人视频
     * 0 未收藏  1  已收藏
     * @param userId
     * @param oLiveVideo
     */
    private void islike(Integer userId, OLiveVideo oLiveVideo) {
        OUserLive oUserLive = new OUserLive();
        oLiveVideo.setUserId(userId);
        List<OUserLive> userLiveList = oUserLiveService.selectOUserLiveList(oUserLive);
        if (userLiveList.size() == 0){
            oLiveVideo.setIsLike("0");//未收藏
        }else {
            oLiveVideo.setIsLike("1");//已收藏
        }
    }
}
